import Vue from 'vue'
import Vuex from 'vuex'
import qs from 'qs'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    authUser:{},//用户信息
    role: '管理员',//角色
    token:'',



    openTab:[],//所有打开的路由
    activeIndex:'/admin' //激活状态
  },
  mutations: {
    set_authUser(state,res){
      this.state.authUser=res.authUser;
      this.state.token=res.token;
      sessionStorage.setItem("authUser",qs.stringify(res.authUser));
      sessionStorage.setItem("token",this.state.token);
    },
    removeAuthUser(){
      this.state.authUser = {};
      this.state.token = '';
      sessionStorage.removeItem('authUser')
      sessionStorage.removeItem('token')
    },
    // 添加tabs
    add_tabs (state, data) {
      this.state.openTab.push(data);
    },
    // 删除tabs
    delete_tabs (state, route) {
      let index = 0;
      for (let option of state.openTab) {
        if (option.route === route) {
          break;
        }
        index++;
      }
      this.state.openTab.splice(index, 1);
    },
    // 设置当前激活的tab
    set_active_index (state, index) {
      this.state.activeIndex = index;
    },
  },
  actions: {

  }
})
